<?php

namespace app\user\logic;

use app\model\InOutBarn;

class InOutBarnLogic
{
    protected InOutBarn $model;
    protected int $factory_id;

    public function __construct()
    {
        $this->model = new InOutBarn();
        $this->factory_id = request()->user_info['factory_id'];
    }

    /**
     * @Description 列表
     * @author: zj
     * @param array $param
     * @time 2024/03/29 15:09:42
     * @return mixed
     */
    public function getList(array $param)
    {
        $query = $this->model->alias('iof')
            ->join('pig p','iof.pig_id = p.out_id','left')
            ->join('fence fe','fe.out_id = p.out_fence_id','left')
            ->join('barn b','b.out_id = fe.out_barn_id','left')
            ->field('iof.out_id as id,iof.pig_id,p.tag_address,p.out_fence_id as fence_id,fe.name as fence,fe.out_barn_id as barn_id,b.name as barn,iof.type,iof.record_time')
            ->where('iof.factory_id',$this->factory_id)
            ->where('iof.delete_time',$this->model::IS_DELETE_NO);
        if ($param['barn_id']) {
            $query->where('fe.out_barn_id',$param['barn_id']);
        }
        if ($param['fence_id']) {
            $query->where('p.out_fence_id',$param['fence_id']);
        }
        if ($param['record_time_start']) {
            $query->where('iof.record_time','>=',strtotime($param['record_time_start']));
        }
        if ($param['record_time_end']) {
            $query->where('iof.record_time','<=',strtotime($param['record_time_end']));
        }

        return $query->order('iof.out_id DESC')
            ->paginate([
                'list_rows' => $param['page_size'],
                'page' => $param['page']
            ])
            ->toArray();
    }
}